import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import java.util.*;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
/*
 *Lop chinh xu ly game */
public class MainGame extends JPanel implements Runnable, Commons {

	private Dimension d;
	private ArrayList<Poultry> arr_poultry;

	private Player player;

	private Thread animator;
	private int speeddropping = 2;// toc do roi cua trung va shit

	ImageIcon nen = new ImageIcon(getClass().getResource("Images/back.png"));
	ImageIcon egg1 = new ImageIcon(getClass().getResource("Images/egg.png"));
	ImageIcon shit = new ImageIcon(getClass().getResource("Images/shit.png"));
	ImageIcon egg2 = new ImageIcon(getClass().getResource("Images/egg2.png"));
	
	int countegg = 0;
	int countshit = 0;
	private int countmiss = 0;

	Rectangle startButton = new Rectangle(G_WIDTH / 2 - 100,G_HIEGHT / 2 - 120, 200, 35);
	Rectangle guideButton = new Rectangle(G_WIDTH / 2 - 100, G_HIEGHT / 2 - 50,	200, 35);
	Rectangle quitButton = new Rectangle(G_WIDTH / 2 - 100, G_HIEGHT / 2 + 20,	200, 35);
	Rectangle okButton = new Rectangle(G_WIDTH / 2 - 100, 340, 200, 35);
	boolean startHover;
	boolean guideHover;
	boolean quitHover;
	boolean okHover;
	boolean showGuide = false;
	boolean showMainMenu = true;
	boolean showGame = false;

	public MainGame() {
		setFocusable(true);
		d = new Dimension(G_WIDTH, G_HIEGHT);
		setBackground(Color.white);

		gameInit();
		this.addKeyListener(new KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_G) {

					addPoultry(1);
				}
				if (evt.getKeyCode() == KeyEvent.VK_V) {
					addPoultry(2);
				}
				if (evt.getKeyCode() == KeyEvent.VK_N) {
					addPoultry(3);

				}
			}
		});
		this.addMouseListener(new MouseHandler());
		this.addMouseMotionListener(new MouseHandler());

		
	}

	// khoi tao game
	public void gameInit() {

		arr_poultry = new ArrayList<Poultry>();

		player = new Player();

		if (animator == null) {
			animator = new Thread(this);
			animator.start();
		}
	}
	// ve gia cam
	public void drawPoultry(Graphics g) {

		for (int i = 0; i < arr_poultry.size(); i++) {
			g.drawImage(arr_poultry.get(i).getImage(), arr_poultry.get(i)
					.getX(), arr_poultry.get(i).getY(), this);

		}
	}
	// ve cai Ro
	public void drawPlayer(Graphics g) {

		g.drawImage(player.getImage(), player.getX(), player.getY(), this);

	}
	// ve trung hoac shit 
	public void drawBombing(Graphics g) {

		for (int i = 0; i < arr_poultry.size(); i++) {
			Poultry a = arr_poultry.get(i);

			Poultry.Bomb b = a.getBomb();

			if (b.isVisible()) {
				g.drawImage(b.getImage(), b.getX(), b.getY(), this);

			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		g.setColor(Color.white);
		g.fillRect(0, 0, d.width, d.height);
		g.drawImage(nen.getImage(), 0, 0, this);

		// Menu
		if (!showGame) {
			g.setFont(new Font("Arial", Font.BOLD, 50));
			g.setColor(Color.WHITE);
			g.drawString("Egg Catcher Game!", 160, 75);

			if (showMainMenu) {
				// Main menu
				if (!startHover)
					g.setColor(Color.CYAN);
				else
					g.setColor(Color.PINK);
				g.fillRect(startButton.x, startButton.y, startButton.width,	startButton.height);
				g.setFont(new Font("Arial", Font.BOLD, 30));

				g.setColor(Color.GRAY);
				g.drawString("Start Game", startButton.x + 20,
						startButton.y + 30);
				if (!quitHover)
					g.setColor(Color.cyan);
				else
					g.setColor(Color.PINK);
				g.fillRect(quitButton.x, quitButton.y, quitButton.width,quitButton.height);
				g.setColor(Color.GRAY);
				g.drawString("Quit Game", quitButton.x + 20, quitButton.y + 30);
				if (!guideHover)
					g.setColor(Color.cyan);
				else
					g.setColor(Color.PINK);
				g.fillRect(guideButton.x, guideButton.y, guideButton.width,	guideButton.height);
				g.setColor(Color.GRAY);
				g.drawString("    Guide", guideButton.x + 20,guideButton.y + 30);
			}
			if (showGuide) {
				// Guide
				g.setFont(new Font("Arial", Font.BOLD, 25));
				g.setColor(Color.WHITE);
				g.drawString("* Press G to add a chicken", 70, 120);
				g.drawString("* Press V to add a duck", 70, 160);
				g.drawString("* Press N to add a geese", 70, 200);
				g.drawString("* Use the mouse to control the basket catch eggs.",70, 240);
				g.drawString("* If you catch shit 3 times or dropping eggs 3 times ",70, 280);
				g.drawString("then the game finishes.", 70, 320);
				if (!okHover)
					g.setColor(Color.cyan);
				else
					g.setColor(Color.PINK);
				g.fillRect(okButton.x, okButton.y, okButton.width,
						okButton.height);
				g.setColor(Color.GRAY);
				g.setFont(new Font("Arial", Font.BOLD, 30));
				g.drawString("      OK", okButton.x + 20, okButton.y + 30);
			}

		}
		// game
		else {

			g.drawLine(0, WIRE, G_WIDTH, WIRE);
			drawPoultry(g);
			drawBombing(g);

		}
		g.drawImage(egg1.getImage(), 205, G_HIEGHT - 75, this);
		g.drawImage(egg2.getImage(), 355, G_HIEGHT - 75, this);
		g.drawImage(shit.getImage(), 505, G_HIEGHT - 75, this);
		g.setColor(Color.black);

		Font font = new Font("Serif", Font.BOLD, 50);
		g.setFont(font);

		g.drawString("" + countegg, 150, G_HIEGHT - 40);
		g.drawString("" + countmiss, 300, G_HIEGHT - 40);
		g.drawString("" + countshit, 450, G_HIEGHT - 40);

		drawPlayer(g);

	}
	// them gia cam
	public void addPoultry(int type) {

		int x = (int) (1 + (G_WIDTH - POULTRY_WIDTH) * Math.random());
		Poultry pouptry = new Poultry(type, 400, WIRE - POULTRY_HEIGHT);

		arr_poultry.add(pouptry);
		pouptry.handlePoultry(0, 800);

	}

	public void animationCycle() {
		for (int i = 0; i < arr_poultry.size(); i++) {
			Poultry a1 = arr_poultry.get(i);
			//gia cam di chuyen
			a1.move(a1.direction);
			
			// bombs
			Poultry.Bomb b = a1.getBomb();
			//de trung hoac shit 
			if (a1.spawnEgg) {
				a1.spawnEgg = false;
				b.setVisible(true);
				b.setX(a1.getX() + POULTRY_WIDTH / 2);
				b.setY(a1.getY() + POULTRY_HEIGHT - 5);
			}

			int bombX = b.getX();
			int bombY = b.getY();
			int playerX = player.getX();
			int playerY = player.getY();
			
			//  hung trung hoac shit
			if (bombX+BOM_WiDTH/2 >= (playerX) && bombX+BOM_WiDTH/2 <= (playerX + PLAYER_WIDTH)
					&& bombY + BOM_HEIGHT >= (playerY)
					&& bombY + BOM_HEIGHT <= (playerY + 30) && b.isVisible()) {
				b.setVisible(false);
				//hung duoc trung
				if (b.typeBom == 1) {
					countegg++;
					if (countegg % 20 == 0)
						speeddropping++;

				}
				//hung duoc shit
				else {
					countshit++;
					if (countshit == 3)
						finishGame();

				}

			}

			if (b.isVisible()) {
				// trung hoac shit roi xuong
				b.setY(b.getY() + speeddropping);

				// ko hung duoc trung
				if (b.getY() >= GROUND) {
					b.setVisible(false);
					if (b.typeBom == 1) {
						countmiss++;
						if (countmiss == 3)
							finishGame();
					}

				}
			}
		}

		
		

	}
	//finishGame thi khoi tao lai mang gia cam va tro lai Menu Game
	public void finishGame() {

		arr_poultry = new ArrayList<Poultry>();
		showMainMenu = true;
		showGame = false;
	}

	public void run() {

		long beforeTime, timeDiff, sleep;

		beforeTime = System.currentTimeMillis();
		//vong lap game
		while (true) {
			repaint();

			animationCycle();

			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = DELAY - timeDiff;
			if (sleep < 0) {
				sleep = 2;
			}
			try {

				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				System.out.println("interrupted");
			}
			beforeTime = System.currentTimeMillis();
		}

	}

	/*
	 * cac su kien mouse*/
	public class MouseHandler extends MouseAdapter {
		@Override
		public void mouseMoved(MouseEvent e) {
			int mx = e.getX();
			int my = e.getY();
			// di chuyen chuot vao startbutton thi doi mau
			if (mx > startButton.x && mx < startButton.x + startButton.width
					&& my > startButton.y
					&& my < startButton.y + startButton.height) {
				startHover = true;

			} else {
				startHover = false;
			}
			// di chuyen chuot vao quitbutton thi doi mau
			if (mx > quitButton.x && mx < quitButton.x + quitButton.width
					&& my > quitButton.y
					&& my < quitButton.y + quitButton.height) {
				quitHover = true;

			} else {
				quitHover = false;
			}
			// di chuyen chuot vao guidebutton thi doi mau
			if (mx > guideButton.x && mx < guideButton.x + guideButton.width
					&& my > guideButton.y
					&& my < guideButton.y + guideButton.height) {
				guideHover = true;

			} else {
				guideHover = false;
			}
			// di chuyen chuot vao oktbutton thi doi mau
			if (mx > okButton.x && mx < okButton.x + okButton.width
					&& my > okButton.y && my < okButton.y + okButton.height) {
				okHover = true;

			} else {
				okHover = false;
			}
			
			//di chuyen Ro theo chuot theo phuong ngang
			int x = (int) e.getPoint().getX() - PLAYER_WIDTH / 2;
			if (x < 0)
				x = 0;
			if (x + POULTRY_WIDTH > G_WIDTH)
				x = G_WIDTH - PLAYER_WIDTH;
			player.setX(x);
		}

		@Override
		public void mousePressed(MouseEvent e) {
			int mx = e.getX();
			int my = e.getY();
			// nhan vao startbutton thi bat dau tro choi
			if (mx > startButton.x && mx < startButton.x + startButton.width
					&& my > startButton.y
					&& my < startButton.y + startButton.height) {
				if (showMainMenu) {
					showMainMenu = false;
					showGame = true;

					countegg = 0;
					countmiss = 0;
					countshit = 0;
					addPoultry(1);
				}

			}
			// nhan vao quitbutton thi dong ung dung
			if (mx > quitButton.x && mx < quitButton.x + quitButton.width
					&& my > quitButton.y
					&& my < quitButton.y + quitButton.height) {
				if (showMainMenu) {
					System.exit(0);
				}

			}
			// nhan vao guidebutton de xem huong dan
			if (mx > guideButton.x && mx < guideButton.x + guideButton.width
					&& my > guideButton.y
					&& my < guideButton.y + guideButton.height) {
				if (showMainMenu) {
					showGuide = true;
					showMainMenu = false;
				}

			}
			// nhan vao okbutton de tro lai Menu Game
			if (mx > okButton.x && mx < okButton.x + okButton.width
					&& my > okButton.y && my < okButton.y + okButton.height) {
				if (showGuide) {
					showGuide = false;
					showMainMenu = true;
				}

			}
		}
	}

}
